<template>
  <div>
    <div>文章类型</div>
    <div class="headWord">文章管理/文章类型</div>
    <div class="headWord">
      <span>类型：</span
      ><el-input v-model="type" placeholder="请输入内容"></el-input>
      <span>状态：</span>
      <el-select v-model="status" placeholder="请选择">
        <el-option
          v-for="item in statuses"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <button class="choose" @click="choose">筛选</button>
      <button class="add" @click="add">添加</button>
    </div>
    <template>
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="name" label="类型名称" width="280">
        </el-table-column>
        <el-table-column prop="time" label="创建时间" width="280">
        </el-table-column>
        <el-table-column prop="orders" label="排序" width="280"> </el-table-column>
        <el-table-column prop="status" label="状态" width="280"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >添加下级</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
     <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1,2, 5, 7, 10]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 模态框 -->
    <el-drawer title="我是标题" :visible.sync="drawer" :with-header="false">
      <div class="editBox">
        <div class="edit">{{ drawerAdd ? "编辑" : "添加" }}</div>
        <p>类型名称:</p>
        <input
          class="editInt"
          type="text"
          placeholder="请输入类型名称"
          v-model="edit.name"
        />
        <p>排序:</p>
        <input
          class="editInt"
          type="text"
          placeholder="请输入排序数字（数字越大越靠前）"
          v-model="edit.orders"
        />
        <p>状态:</p>
        <el-radio v-model="edit.status" label="启用">启用</el-radio>
        <el-radio v-model="edit.status" label="禁用">禁用</el-radio>
        <div class="btn">
          <el-button type="primary" @click="putTrue">确认</el-button>
          <el-button type="info" @click="FalseT">取消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import axios from "axios"
export default {
  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
      type: "",
      status: "",
      statuses: [
        {
          value: "启用",
          label: "启用",
        },
        {
          value: "禁用",
          label: "禁用",
        },
      ],
      currentPage: 1,
      pagesize: 2,
      total:0,
      list:[],
      drawer: false,
      drawerAdd: true,
      edit: {},
      options: [
        {
          value: "启用",
          label: "启用",
        },
        {
          value: "禁用",
          label: "禁用",
        },
      ],
      value: "",
      type: "",
    };
  },
  methods: {
    getList(currentPage, pagesize) {
      axios
        .get(
          `http://192.168.31.39:9101/essaytype/findAll/${currentPage}/${pagesize}`
        )
        .then((res) => {
          this.total = res.data.data.total;
          this.list = res.data.data.data;
          this.list.map((item) => {
            if (item.status == 1) {
              item.status = "启用";
            } else {
              item.status = "禁用";
            }
          });
          console.log(res);
        });
    },
    handleEdit(index, row) {
      this.drawer=true
      this.edit={...row}
    },
    //消息提示
    open2(msg) {
      this.$message({
        message: `${msg}成功`,
        type: "success",
      });
    },
    //删除
    handleDelete(index, row) {
      console.log(index, row.essayTypeId);
        axios
        .get(`http://192.168.31.39:9101/essaytype/del/${row.essayTypeId}`)
        .then((res) => {
          if (res.data.code == 200) {
            this.getList(this.currentPage, this.pagesize);
            this.open2("删除");
          }
        });
    },
     handleSizeChange(val) {
      this.pagesize = val;
      this.getList(this.currentPage, this.pagesize);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList(this.currentPage, this.pagesize);
    },
    //修改
    editList(data) {
      axios({
        method: "post",
        url: "http://192.168.31.39:9101/essaytype/update",
        data,
      }).then((res) => {
        if (res.data.code == 200) {
          this.open2("修改");
          this.getList(this.currentPage, this.pagesize);
          this.drawer = false;
          this.edit = {};
        }
      });
    },
    //确认修改
    putTrue() {
      let list = { ...this.edit };
      if (this.edit.status == "启用") {
        list.status = 1;
      } else {
        list.status = 0;
      }
      if (this.drawerAdd) {
        this.editList(list);
      } else {
        this.addT(list);
      }
      this.edit={}
    },
    FalseT() {
      this.drawer = false;
      this.drawerAdd = true;
      this.edit={}
    },
    add(){
      this.drawer=true
      this.drawerAdd=false
    },
    addT(list) {      
      axios({
        url: "http://192.168.31.39:9101//essaytype/add",
        method: "post",
        data: list,
      }).then(res=>{
        if (res.data.code==200) {
          this.getList(this.currentPage, this.pagesize);
          this.open2("添加")
        }
      })
      this.drawer = false;
      this.drawerAdd = true;
    },
     //筛选
    choose() {
      let val;
      if (this.status == "启用") {
        val = 1;
      } else {
        val = 0;
      }
      let obj = { type: this.type, status: val };
      // console.log(obj);
      axios({
        url: "http://192.168.31.39:9101/essaytype/findByTypeAndStatus",
        method: "post",
        data: obj,
      }).then((res) => {
        console.log("saixuan",res);
        if (res.data.code == 200) {
          this.list = res.data.data;
          this.type = "";
          this.value = "";
        }
      });
    },
  },
  created() {
    this.getList(this.currentPage, this.pagesize);
  },
};
</script>
<style lang="">
.choose {
  background-color: rgb(48, 208, 198);
  color: white;
  border: none;
  height: 40px;
  width: 50px;
  margin-left: 10px;
}
.headWord {
  margin: 10px 0;
  position: relative;
}
.add{
  position: absolute;
  right: 0;
  background-color: rgb(48, 208, 198);
  color: white;
  border: none;
  height: 40px;
  width: 60px;
}
.el-input{
  width: 200px;
}
.edit {
  text-align: center;
}
.editBox {
  padding: 20px;
}
.editInt {
  width: 100%;
  height: 30px;
}
.btn {
  margin-top: 20px;
  text-align: center;
}
</style>